package com.xxx.video.mine.presenters;

import android.content.Context;

import androidx.annotation.NonNull;

import com.xxx.video.commen.dbs.beans.SearchHistoryBean;
import com.xxx.video.commen.dbs.utils.DbUtils;
import com.xxx.video.commen.presenters.BasePresenter;
import com.xxx.video.commen.utils.ResponseUtils;
import com.xxx.video.main.beans.SearchListBean;
import com.xxx.video.mine.beans.SearchHotWordListBean;
import com.xxx.video.mine.beans.SearchRecommendListBean;
import com.xxx.video.network.listeners.CommonResponseListener;
import com.xxx.video.network.utils.NetWorkUtils;

public class SearchResultListPresenter extends BasePresenter<ISearchResultListView> {

    public SearchResultListPresenter(@NonNull Context context, ISearchResultListView baseView) {
        super(context, baseView);
    }

    /**
     * search
     *
     * @param searchContent content
     */
    public void search(String searchContent, int page) {
        SearchHistoryBean searchHistoryBean = new SearchHistoryBean.Builder()
                .createTime(System.currentTimeMillis())
                .searchWord(searchContent)
                .build();
        DbUtils.getInstance().addSearchHistoryBean(searchHistoryBean);
        NetWorkUtils.getInstance().search(searchContent, String.valueOf(page), new CommonResponseListener<SearchListBean>() {
            @Override
            public void onSuccess(SearchListBean successOb, String result) {
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        getBaseView().initData2View(successOb);
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {

            }
        });
    }

    /**
     * get history key word
     */
    public void getHistoryKeyword() {
        NetWorkUtils.getInstance().getHistoryKeyword(new CommonResponseListener<SearchHotWordListBean>() {
            @Override
            public void onSuccess(SearchHotWordListBean successOb, String result) {
                if (getBaseView() != null) {
                    getBaseView().showRootLayoutStatus(1);
                }
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        getBaseView().initSearchHotWordListBeanData2View(successOb);
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {
                if (getBaseView() != null) {
                    getBaseView().showRootLayoutStatus(1);
                }
            }
        });

    }
    /**
     * get recommend list
     */
    public void getRecommendList() {
        NetWorkUtils.getInstance().getRecommendList(new CommonResponseListener<SearchRecommendListBean>() {
            @Override
            public void onSuccess(SearchRecommendListBean successOb, String result) {
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        getBaseView().initRecommendData2View(successOb);
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {

            }
        });
    }
}
